package com.lixw.aiassistant.handler;

import com.lixw.aiassistant.tool.OrderFunctionTool;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID;


/**
 * @ClassName: KnowledgeChainHandler
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/13 20:43
 **/
@Component
public class BusinessChainHandler extends AbstractChainHandler{

    @Autowired
    private VectorStore vectorStore;
    @Value("classpath:/promptTemplate_long.txt")
    private Resource promptTemplateResource;
    @Autowired
    private OrderFunctionTool orderFunctionTool;

    @Override
    protected Flux<String> doHandle(String sessionId, String enhancedPrompt) {
        PromptTemplate promptTemplate = new PromptTemplate(promptTemplateResource);
        Prompt systemPrompt = promptTemplate.create();

        return chatClient.prompt()
                .system(systemPrompt.getContents())
                .user(enhancedPrompt)
                .tools(orderFunctionTool)
                //记忆存储
                .advisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
                .advisors(advisorSpec -> advisorSpec.param(CONVERSATION_ID, sessionId))
                //工具注册
                .stream()
                .content();
    }
}